<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>计算总成绩</title>
</head>
<body>
    <script>
        // 定义一个函数来获取有效的输入
        function getValidNumber(promptMessage) {
            let num;
            do {
                num = parseInt(prompt(promptMessage), 10);
                if (isNaN(num) || num <= 0) {
                    alert("输入的不是数字");
                    num = null; // 使循环继续
                }
            } while (num === null);
            return num;
        }

        // 获取科目数量
        const subjectCount = getValidNumber("请输入考试科目的数量：");

        let totalScore = 0;

        // 循环获取每个科目的成绩
        for (let i = 1; i <= subjectCount; i++) {
            let score;
            do {
                score = parseFloat(prompt(`请输入第${i}门科目的成绩：`));
                if (isNaN(score) || score < 0) {
                    alert("成绩不能是负数");
                    score = null; // 使循环继续
                }
            } while (score === null);
            totalScore += score;
        }

        // 显示总成绩
        alert(`所有科目的总成绩为：${totalScore}`);
    </script>
</body>
</html>